/*
 * SystemTime.h
 *
 *  Created on: 2010-8-12
 *      Author: stargui
 */

#ifndef SYSTEMTIME_H_
#define SYSTEMTIME_H_
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>


namespace Common
{


//公用时间结构体
typedef struct _datetime_{
	unsigned char year; 	/**< from 2000 */
	unsigned char month; /**< 1-12 */
	unsigned char day; 	/**< 1~31 */
	unsigned char hour;	/**< 0~23,0=24:00 */
	unsigned char minute;/**< 0~59 */
	unsigned char second;/**< 0~59 */
	unsigned char week;	/**< 0~6 0=sunday*/
	char reserved;
}datetime_t;

//读取RTC时钟时用到结构体
typedef struct
{
    unsigned int  second; /**< 0~59*/
    unsigned int  minute; /**< 0~59*/
    unsigned int  hour; /**< 0~23*/
    unsigned int  date; /**< 1~31 */
    unsigned int  weekday; /**< 0~6 0=sunday */
    unsigned int  month; /**< 1~12 */
    unsigned int  year; /**< from 2000 */
} rtc_time_t;

class CSystemTime
{

public:

	CSystemTime();

	virtual ~CSystemTime();

public:

	static uint64_t GetSystemMicroSecond(void);

	static int SystimeToRtctime( const datetime_t* pstuLocalTime, rtc_time_t* pstuRtcTime, int iZoneMin, int bIsDst );

	static int RtctimeToSystime( const rtc_time_t* pstuRtcTime, datetime_t* pstuLocalTime, int iZoneMin, int bIsDst );

	static int UtcTimeToDateTime(const time_t *pUtcTime, datetime_t *pDateTime);

	static int DateTimeToUtcTime(const datetime_t *pDateTime, time_t *pUtcTime);

#if 0
	static time_t GetSystemSecond(void);

	static time_t GetLocalSecond();

	static int SetSystemTime( rtc_time_t stuRTCTime, int iZoneMin, int bIsDst );

	static int GetSystemTime( datetime_t* stuLocalTime );
	/**
	 * @brief check the date pstuSystime weather in dst time
	 * <b>pstuSystime must not a dst time</b>
	 *  ucStartWeek/ucEndWeek 高四位表示第几个星期（0：第一个星期1：第二星期 2：第三星期 4：第四星期 5：最后一星期），低四位表示星期几（0-星期日，1-6表示星期一到六）；
	 * @return true 已到夏令时时间
	 * */
	static bool CheckDateIsInDst( datetime_t* pstuSystime, uint8_t ucStartMonth, uint8_t ucStartWeek, uint8_t ucEndMonth, uint8_t ucEndWeek
			,uint8_t ucStartHour, uint8_t ucStartMinute, uint8_t ucStartSecond, uint8_t ucEndHour, uint8_t ucEndMinute, uint8_t ucEndSecond );

	static bool CheckDateIsInDst( datetime_t* pstuSystime, uint32_t ulStartTime, uint32_t ulEndTime );

	static int ChangeSystemTimezone( int iZoneMin );

	static int ChangeSystemDstState( int bisDst );

	static int ChangeSystemTimeUTC( rtc_time_t stuRTCTime );
#endif

public:
	/**
	 * @brief 设置共享时间
	 * @param pstuDvrTime 时间，DVR时间（经过时区，夏令时转换后的 ）
	 * @return 无效
	 * */
	static int SetShareTime( const datetime_t* pstuDvrTime, int iTimeZone, int iDstOffHour );

	/**
	 * @brief 获取共享时间
	 * @param pstuDvrTime 时间，DVR时间
	 * @return 无效
	 * */
	static int GetShareTime( datetime_t* pstuDvrTime );

	/**
	 * @brief 获取本地时间从1970年经过的秒
	 * */
	static time_t GetShareSecond();

	/**
	 * @brief 获取UTC从1970年经过的秒(UTC)
	 * */
	static time_t GetShareUTCSecond();
};

}

#endif /* SYSTEMTIME_H_ */
